do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"


use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear

local suff2 = ""

keep if estsampall`suff2'==1

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}


keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
  mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter

global teachvar = "commute_time commute_time_miss va_ma_tot`suff2' frac_black frac_hisp n_disadv mn_achievement blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2'"
 global teachvar_cm = "va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2'"
 
* vary: (none x teacher FEs vs teacher correlated REs) x (none x school FEs vs school REs)

logit applied $teachvar $teachvar_cm if choiceset==1
foreach var in $teachvar $teachvar_cm {
	gen b1_`var' = _b[`var']
	gen se1_`var' = _b[`var']
}
predict xb1, xb


xtset itidx  

xtlogit applied $teachvar $teachvar_cm i.schoolidx  if choiceset==1 , re iterate(50)
foreach var in $teachvar {
	gen b2_`var' = _b[`var']
	gen se2_`var' = _b[`var']
}
predict xb2, xb

xtlogit applied $teachvar  i.schoolidx  if choiceset==1 , fe iterate(50)
foreach var in $teachvar {
	gen b3_`var' = _b[`var']
	gen se3_`var' = _b[`var']
}
predict xb3, xb

keep b* se* xb*

save "$temp/binary_logit_results", replace


use  "$temp/binary_logit_results", clear

forv m=1/3 {
preserve
keep b`m'_commute_time xb`m'
order b
outsheet using "$temp/binary_logit`m'.csv", comma replace nonames
restore
}
	